package Beginning;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Button;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.awt.Color;
import java.awt.SystemColor;

public class Form1 {

	private JFrame frame;
	private JTextField txtA;
	private JTextField txtB;
	private JTextField txtS;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Form1 window = new Form1();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Form1() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setForeground(new Color(0, 0, 255));
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		txtA = new JTextField();
		txtA.setColumns(10);
		
		txtB = new JTextField();
		txtB.setColumns(10);
		
		txtS = new JTextField();
		txtS.setEnabled(false);
		txtS.setForeground(new Color(0, 0, 0));
		txtS.setBackground(SystemColor.inactiveCaptionBorder);
		txtS.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Nh\u1EADp a:");
		
		JLabel lblNhpB = new JLabel("Nh\u1EADp b:");
		
		JLabel lblTngL = new JLabel("T\u1ED5ng l\u00E0:");
		
		JButton btnS = new JButton("T\u00EDnh");
		btnS.setForeground(Color.BLACK);
		btnS.setToolTipText("Tính tổng 2 số nhập ở trên");
		btnS.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane abc = new JOptionPane();
		    	String strText1 = txtA.getText();
		    	String strText2 = txtB.getText();
		    	if(!strText1.equals("") && !strText2.equals(""))
		    	{
		    		if(isNumeric(strText1) && isNumeric(strText2))
		    		{
		    			int sum = Integer.parseInt(strText1) + Integer.parseInt(strText2);
		        		txtS.setText(Integer.toString(sum));
		    		}
		    		else
		    		{
		    			abc.showMessageDialog(null, "Chỉ được nhập số nguyên");
		    		}
		    	}
		    	else
		    	{
		    		abc.showMessageDialog(null, "Vui lòng nhập đủ 2 số");
		    	}
			}
		});
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap(72, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblNewLabel)
								.addComponent(lblNhpB)
								.addComponent(lblTngL))
							.addGap(54)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(txtS, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
								.addComponent(txtB)
								.addComponent(txtA))
							.addGap(71))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnS)
							.addGap(165))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(44)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtA, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addGap(29)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNhpB))
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtS, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTngL))
					.addGap(30)
					.addComponent(btnS)
					.addContainerGap(44, Short.MAX_VALUE))
		);
		frame.getContentPane().setLayout(groupLayout);
	}
	public static boolean isNumeric(String str)
    {
      NumberFormat formatter = NumberFormat.getInstance();
      ParsePosition pos = new ParsePosition(0);
      formatter.parse(str, pos);
      return str.length() == pos.getIndex();
    }
}
